import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'dart:math' as math;

class DemoPlugin extends StatefulWidget {
  const DemoPlugin({super.key, this.width = 180, this.height = 320});

  final double width;
  final double height;

  @override
  State<StatefulWidget> createState() {
    return DemoPluginState();
  }
}

class DemoPluginState extends State<DemoPlugin> {
  final methodChannel = const MethodChannel('demo_plugin');
  final eventChannel = const EventChannel('demo_plugin_event');
  int textureId = -1;

  Future<String?> getPlatformVersion() async {
    final version =
        await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  Future<int?> getTextureId() async {
    final textureId = await methodChannel.invokeMethod<int>('getTextureId');
    return textureId;
  }

  Future<String?> getScanResult() async {
    final code = await methodChannel.invokeMethod<String>('getScanResult');
    return code;
  }

  @override
  void initState() {
    super.initState();
    getTextureId().then((value) {
      setState(() {
        textureId = value!;
        getScanResult();
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Transform.rotate(
        angle: math.pi / 2,
        alignment: Alignment.center,
        child: Container(
          height: widget.width,
          width: widget.height,
          decoration: const BoxDecoration(
            color: Color(0xFF00FF00), // 背景色
          ),
          child: textureId >= 0
              ? Texture(textureId: textureId)
              : const Text('loading...'),
        ),
      ),
    );
  }
}
